<?php
/**
 * Created by PhpStorm.
 * User: sunfan
 * Date: 2017/3/28
 * Time: 13:15
 */

namespace Admin\Controller;


use Admin\Model\EnterpriseModel;

class SpellOrderController extends AdminController
{

    public function index()
    {

        $statusarr[0]='待付款';
        $statusarr[1]='待发货';
        $statusarr[2]='已发货';
        $statusarr[3]='已完成';
        $statusarr[4]='已取消';
        $this->assign('statusarr',$statusarr);

        $status = I('status');
        $isdraw = I('isdraw');
        $map = array('onethink_merge.type'=>1);
        $excel_url = U('orders_excel');
        if (!empty($id)) {
            $map['sn'] = array('like',"%$id%");
            $excel_url .= "&sn=".$id;
        }

        $stat = I('time_start');
        $end = I('time_end');
        if ($stat){
            $start = strtotime($stat);
            $map['onethink_order.addtime'] = array('egt',$start);
            $excel_url .="&start=$start";
        }
        if ($end){
            $endt = strtotime($end." 23:59:59");
            $map['onethink_order.addtime'] = array('elt',$endt);
            $excel_url.="&endtime=$endt";
        }
        if ($stat&&$end){
            $start = strtotime($stat);
            $endt = strtotime($end." 23:59:59");
            $map['onethink_order.addtime'] = array('between',$start.",".$endt);
        }
        if ($status!="") {
            $map['status'] = (int)$status;
            $excel_url.="&status=$status";
        }
        $userids = M('user')->getField('id',true);
        $mobile = I('mobile');
        if ($mobile!=""){
            $userids = M('user')->where('mobile like "%'.$mobile.'%"')->getField('id',true);
        }
        $userid = I('user_id');
        if($userid!="") $userids = $userid;
        $map['onethink_order.user_id'] = array('in',$userids);
        $where['onethink_order.user_id'] = array('in',$userids);
        $excel_url.="&userid=".implode(',', $userids);

        if($status!=""&&$isdraw=="") {
            $map['onethink_order.is_drawback'] = 0;
            $excel_url.="&drawback=0";
        }

        if ($isdraw!="") {
            $map['onethink_order.is_drawback'] = $isdraw;
            $excel_url.="&drawback=".$isdraw;
        }
        $count = M('order')->where($map)->count();

        $p = new \Think\Page($count, 20);
        $list = M('order')->limit($p->firstRow.','.$p->listRows)->where($map)->order('id DESC')->select();
        foreach ($list as $k=>$v){
            $list[$k]['name'] = M('user')->where('onethink_user.id='.$v['user_id'])->getField('name');
        }

        //已退款订单数量
        $where['onethink_order.is_drawback'] = 1;
        $where['onethink_order.is_del'] = 0;
        $drawbacknum = M('order')->where($where)->count();
        $this->assign('drawbacknum',$drawbacknum);

        $list = D('order')->
        join('onethink_merge ON onethink_order.merge_id = onethink_merge.merge_id')->
        join('onethink_user ON onethink_order.user_id = onethink_user.id')->
        where($map)->
        field('onethink_order.merge_id,onethink_order.id,onethink_order.addtime,onethink_order.money,onethink_order.pay_money,onethink_order.sn,onethink_order.status,onethink_order.is_drawback
        ,onethink_user.login_user,onethink_user.mobile,onethink_user.worker_id,onethink_user.section,onethink_user.company_id')->
        select();
        $enterprise = new EnterpriseModel();
        foreach ($list as $k => $v){
           $list[$k]['company'] =  $enterprise->where('id='.$v->company_id)->field('name')->find()['name'];
           $list[$k]['businessarea'] = D('BusinessRange')->where(array('merge_id'=>$v['merge_id']))->field('area_name')->find()['area_name'];
        }
        $this->assign('list',$list);
        $this->assign('excel_url',$excel_url);
        parent::index(); // TODO: Change the autogenerated stub
    }

    /**
     * 导出订单
     */
    public function orders_excel(){
        $m=M('order');
        $map = array();
        $map['is_del'] = 0;
        $status = I('status');
        if($status!=""){
            $map['status'] = $status;
        }
        $title = I('sn');
        if ($title!=""){
            $map['sn'] = array('like',"%$title%");
        }
        $draw = I('drawback');
        if($draw!=""){
            $map['is_drawback'] = $draw;
        }

        $ids    =  rtrim(I('ids'),',');
        if (!empty($ids)){
            $map['id'] = array('in',$ids);
        }
        $userid = I('userid');
        if($userid!=""){
            $map['user_id'] = array('in',$userid);
        }

        //$join = "onethink_user_address as adr on adr.id=o.address_id";
        //$field = "o.*,adr.name,adr.mobile,adr.area,adr.address";
        $list = $m->where($map)->order("id DESC")->select();
        // 		print_r($m->getLastsql());die;
        $str = '
		<table border="1">
		<tr style="background-color:#ccffcc;min-height:30px;">
		<th align="center">ID</th>
		<th align="center">订单编号</th>
		<th align="center">下单时间</th>
		<th align="center">订单状态</th>
		<th align="center">订单金额</th>
		<th align="center">支付方式</th>
		<th align="center">运费</th>
		<th align="center">取货方式</th>
		<th align="center">用户账号</th>
		<th align="center">收货人</th>
		<th align="center">联系电话</th>
		<th align="center">所在企业</th>
		<th align="center">所属部门</th>
		<th align="center">所属商圈</th>
		
		</tr>
		';
        foreach ($list as $k => $v) {

            $v['name'] = get_username($v['user_id']);
            $v['mobile'] = getusermobile($v['user_id']);
            $user = D('User')->where(array('id'=>$v['user_id']))->field('login_user,name,company_id,section')->find();
            $v['name'] = $user['name'];
            $v['login_user'] = $user['login_user'];
            $v['section'] = $user['section'];
            $v['company'] = D('Enterprise')->where(array('id'=>$user['company_id']))->getField('name');
            $v['businessrange'] = D("merge")->
                join('onethink_business_range on onethink_merge.bu_a_id = onethink_business_range.merge_id')
                ->where(array('id'=>$v['merge_id']))->getField('area_name');
            //查找订单商品信息
            $pros = M('order_detail')->alias("d")->join("LEFT JOIN onethink_product as p on p.id=d.product_id")->where("d.order_id='".$v['id']."'")->order("d.id ASC")->field("d.*,p.title")->select();
            $num = count($pros);
            if (($k%2)==0){
                $str .='<tr align="center" style="background-color:#fdffcc;min-height:30px;">';
            }else{
                $str .='<tr align="center" style="min-height:30px;">';
            }
            $str .= '
           <td rowspan='.$num.'>'.$v['id'].'&nbsp;</td> 
			<td rowspan='.$num.'>'.$v['sn'].'&nbsp;</td>
			<td rowspan='.$num.'>'.date('Y-m-d H:i',$v['addtime']).'</td>';
            if($v['status']==0){
                $str.='<td rowspan='.$num.'>待付款</td>';
            }elseif($v['status']==1){
                if($v['type']==1){
                    $str.='<td rowspan='.$num.'>待自提</td>';
                }else{
                    $str.='<td rowspan='.$num.'>待发货</td>';
                }
            }elseif($v['status']==2){
                $str.='<td rowspan='.$num.'>待收货</td>';
            }elseif($v['status']==3){
                $str.='<td rowspan='.$num.'>交易完成</td>';
            }elseif ($v['status']==4){
                $str.='<td rowspan='.$num.'>交易取消</td>';
            }

            $str .='<td rowspan='.$num.'>'.$v['money'].'&nbsp;</td>';
            if($v['type']==1){
                $str.='<td rowspan='.$num.'>余额支付</td>';
            }else{
                $str.='<td rowspan='.$num.'>微信支付</td>';
            }
            $str.='<td rowspan='.$num.'>'.$v['express_money'].'&nbsp;</td>';
            if($v['type']==1){
                $store = M('store')->where('id='.$v['depot_id'])->find();
                $str.='
				<td rowspan='.$num.'>门店自提</td>
				<td rowspan='.$num.'>&nbsp;</td>
				<td rowspan='.$num.'>&nbsp;</td>
				<td rowspan='.$num.'>&nbsp;</td>
				<td rowspan='.$num.'>&nbsp;</td>
				<td rowspan='.$num.'>'.$store['sname'].'</td>
				<td rowspan='.$num.'>'.$store['mobile'].'&nbsp;</td>';
            }else{
                $str.='
				<td rowspan='.$num.'>送货上门</td>
				<td rowspan='.$num.'>'.$v['login_user'].'</td>
				<td rowspan='.$num.'>'.$v['name'].'</td>
				<td rowspan='.$num.'>'.$v['mobile'].'&nbsp;</td>
				<td rowspan='.$num.'>'.$v['company'].'</td>
				<td rowspan='.$num.'>'.$v['section'].'</td>
				<td rowspan='.$num.'>'.$v['businessrange'].'</td>';
            }

            //$str.='</table></td></tr>';

        }
        $str .="</table>";


// 		print_r($str);die;
        //输出
        header('Content-Length: '.strlen($str));
        header("Content-type:application/vnd.ms-excel;charset=UTF-8");
        header("Content-Disposition:filename=orders.xls");
        echo $str;
    }

    public function del(){
         $id = I("get.id");
         $del = D('order')->where(array('id'=>$id))->delete();
         if($del){
           D('OrderDetail')->where(array('order_id'=>$id))->delete();
           $this->success('订单已删除!',U('SpellOrder/index'));
         }else{
           $this->error("删除失败！");
         }
    }

}